<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <div class="app-container">
      <el-card
        shadow="never"
        class="search-card-box"
      >
        <el-form
          ref="rangeSearchFormData"
          :model="rangeSearchFormData"
          class="search-form"
          label-width="95px"
          :rules="rules"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="员工账号:"
                prop="employeeNumber"
              >
                <el-input
                  v-model="rangeSearchFormData.employeeNumber"
                  placeholder="请输入员工账号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="员工姓名:"
                prop="name"
              >
                <el-input
                  v-model="rangeSearchFormData.name"
                  placeholder="请输入员工姓名"
                  prop="name"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="工作模式:"
                prop="workPatternId"
              >
                <el-select
                  v-model="rangeSearchFormData.workPatternId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item,index) in workOptions"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="月份:"
                prop="month"
              >
                <el-date-picker
                  v-model="rangeSearchFormData.month"
                  type="month"
                  placeholder="选择月"
                  value-format="yyyy-MM"
                  @change="handle"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <!-- 下拉菜单 -->
            <el-col :span="8">
              <el-form-item
                label="所属结构:"
                prop="agentId"
              >
                <treeselect
                  v-model="rangeSearchFormData.agentId"
                  :searchable="true"
                  :options="options"
                  placeholder="请选择所属机构"
                  :normalizer="normalizer"
                  style="width: 100%"
                  @select="selectAgency"
                  @open="open"
                  @close="close"
                />
                <img
                  ref="arrow"
                  src="../../assets/pullDown.png"
                  style="position:absolute;width:18px;height:18px;right:9px;top:12px;cursor:pointer;pointer-events:none;transition-duration:0.3s"
                />
              </el-form-item>
            </el-col>
            <el-col
              :span="8"
              style="margin-top: 0px"
            >
              <el-button
                type="warning"
                @click="searchForm"
              >搜索</el-button>
              <el-button
                plain
                class="reset-btn"
                @click="resetFields"
              >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card
        shadow="never"
        style="margin-top: 20px"
      >
        <el-row>
          <el-button
            type="warning"
            @click="$router.push('/transit/workArrange-detail')"
          >排版设置</el-button>
          <el-button @click="openDialog">绑定排班</el-button>
        </el-row>
        <el-table
          ref="multipleTable"
          style="width: 100%;"
          :data="list"
          @selection-change="handleSelectionChange"
        >
          >
          <el-table-column
            type="selection"
            width="100px"
            fixed="left"
          ></el-table-column>
          <el-table-column
            label="员工账号"
            fixed="left"
            prop="employeeNumber"
          ></el-table-column>
          <el-table-column
            label="员工姓名"
            fixed="left"
            prop="name"
          ></el-table-column>
          <el-table-column
            label="员工电话"
            fixed="left"
            prop="phone"
          ></el-table-column>
          <el-table-column
            label="员工角色"
            fixed="left"
            prop="userType"
          >
            <template v-slot="{ row }">
              <span v-if="row.userType===1">
                员工
              </span>
              <span v-else-if="row.userType===2">
                快递员
              </span>
              <span v-else-if="row.userType===3">
                司机
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="当前工作模式"
            fixed
            prop="workPatternName"
          >
          </el-table-column>
          <el-table-column
            v-for="item in 30"
            :key="item"
            :label="String(item)"
            prop="getPropName(item)"
          >
            <!-- prop="workSchedules.item" -->
            <template #default="{ row }">
              <div>
                <span :class="row.workSchedules[item - 1] === true ? 'true-style' : 'false-style'">{{ row.workSchedules[item - 1] === true ? '休' : '上' }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- <el-table :data="workSchedules">
              <template v-slot="{row}">
                <el-table-column
                  :key="row.id"
                  :label="getIndexLabel(index+1)"
                >
                  {{ row }}
                </el-table-column>
              </template>
            </el-table> -->
        <div class="pagination">
          <div class="pages">
            <el-row
              type="flex"
              justify="center"
            >
              <el-pagination
                :page-size="queryparams.pageSize"
                :page-sizes="[10,20,30,40]"
                layout="total, sizes, prev, pager, next, jumper"
                :current-page="queryparams.page"
                :total="Number(total)"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
              >
              </el-pagination>
            </el-row>
          </div>
        </div>
        <el-dialog :visible.sync="dialogVisible">
          <el-form
            ref="tableForm"
            :model="tableForm"
          >
            <el-form-item prop="workPatternType">
              <template>
                <el-radio
                  v-model="tableForm.workPatternType"
                  label="1"
                >礼拜制</el-radio>
                <el-radio
                  v-model="tableForm.workPatternType"
                  label="2"
                >连续制</el-radio>
              </template>
            </el-form-item>
            <el-form-item
              label="模式名称:"
              prop="workPatternId"
            >
              <el-select
                v-model="tableForm.workPatternId"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item,index) in workOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <span
            slot="footer"
            class="dialog-footer"
          >
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              type="primary"
              @click="peopleSetOK"
            >确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
// import { workSchedulings, workSchedulingsList, workPatternList } from '@/api/work.js'
import { workManage, selectWorkHistoryList, peopleSet } from '@/api/transit'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { agencyList } from '@/api/institutions'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      rangeSearchFormData: {
        name: '',
        phone: '',
        account: '',
        agentId: null,
        employeeNumber: '',
        month: '',
        workPatternId: null,
        page: 1,
        pageSize: 10
      },
      rules: {
        employeeNumber: [],
        mame: [],
        workPatternId: [],
        month: [],
        agentId: []
      },
      value: '',
      options: [],
      // 工作模式下拉菜单
      workOptions: [],
      list: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      // 多选框数组
      multipleTable: [],
      workSchedules: [],
      queryparams: {
        page: 1,
        pageSize: 10
      },
      total: null,
      tableList: [],
      tableForm: {
        userIdList: [],
        userType: '',
        workPatternId: null,
        workPatternType: '1'
      },
      dialogVisible: false,
      peopleSetData: {
        userIdList: [],
        userType: '',
        workPatternId: null,
        workPatternType: ''
      },
      formChanged: null
    }
  },
  computed: {
  },
  created() {
    this.workManage()
    this.agencyList()
    this.selectWorkHistoryList()
  },
  mounted() {
  },
  methods: {
    // 打开所属机构下拉框
    open() {
      this.$refs.arrow.style.transform = 'rotate(-180deg)'
    },
    // 关闭所属机构下拉框
    close() {
      this.$refs.arrow.style.transform = 'rotate(0deg)'
    },
    async selectWorkHistoryList() {
      const res = await selectWorkHistoryList()
      this.workOptions = res.data['1']
    },
    async workManage() {
      try {
        const res = await workManage(this.queryparams)
        this.list = res.data.items
        const arr = this.list.map(item => item.workSchedules)
        this.total = res.data.counts
        this.workSchedules = arr
      } catch (err) {
        console.log(err)
      }
    },
    getIndexLabel(index) {
      return `${index}`
    },
    handleCurrentChange(val) {
      this.queryparams.page = val
      this.workManage()
    },
    handleSizeChange(val) {
      this.queryparams.pageSize = val
      this.$nextTick(this.workManage())
    },
    async agencyList() {
      const res = await agencyList()
      this.options = JSON.parse(res.data)
    },
    selectAgency(val) {
      const res = String(val.id)
      this.rangeSearchFormData.agentId = res
    },
    // =================================搜索按钮事件====================================
    async searchForm() {
      this.$refs.rangeSearchFormData.validate(async isOk => {
        if (isOk) {
          const res = await workManage(this.rangeSearchFormData)
          this.list = res.data.items
        } else {
          this.workManage()
        }
      })
    },
    openDialog() {
      if (this.tableList.length) {
        this.dialogVisible = true
      } else {
        this.$message.error('请绑定员工')
      }
    },
    async resetFields() {
      this.$refs.rangeSearchFormData.resetFields()
      this.rangeSearchFormData = {
        name: '',
        phone: '',
        account: '',
        agentId: null,
        employeeNumber: '',
        month: '',
        workPatternId: null,
        page: 1,
        pageSize: 10
      }
      const res = await workManage(this.queryparams)
      this.list = res.data.items
    },
    // 人工排版
    handleSelectionChange(val) {
      this.tableList = val
      if (this.tableList.length) {
        this.tableList = val.map(item => item.id)
      }
    },
    async peopleSetOK() {
      // console.log(123123)
      try {
        this.tableForm.userIdList = this.tableList
        console.log(this.tableForm)
        const res = await peopleSet(this.tableForm)
        console.log(res)
        this.dialogVisible = false
      } catch (err) {
        console.log(err)
      }
      this.workManage()
    },
    handle(val) {
      console.log(val)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}
.true-style {
 display: inline-block;
 width: 24px;
 height: 24px;
 background: #eff3f8;
 border: 1px solid #d8dde3;
 border-radius: 50%;
 text-align: center;
 color: #20232a;
 font-size: 13px;
}
.false-style {
 display: inline-block;
 width: 24px;
 height: 24px;
 background: #ffeeeb;
 border: 1px solid #e15536;
 border-radius: 50%;
 text-align: center;
 color: #e15536;
 font-size: 13px;
}
.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card{
  overflow: initial;
}

// .search-card {
  // padding-top: 20px;
// }
</style>

<style></style>
